package com.shujia

class Person(id: String, name: String, age: Int) {
  val _id: String = id
  val _name: String = name
  val _age: Int = age

  def sleep(): Unit = {
    println("人类会睡觉")
  }

  def printInfo(): Unit = {
    println(s"Person($id,$name,$age)")
  }

}

// 子类通过extends继承父类，可以继承属性以及方法
// Scala中继承的同时需要调用父类默认的构造方法
class Student(id: String, sId: String, name: String, age: Int) extends Person(id, name, age) {
  // 学生特有的属性
  val _sId: String = sId

  // 学生特有的方法
  def learning(): Unit = {
    println("学生会学习")
  }

  override def sleep(): Unit = {
    println("学生会睡觉")
  }

  override def printInfo(): Unit = {
    println(s"Student($id,$sId,$name,$age)")
  }

}

class Teacher(id: String, tId: String, name: String, age: Int) extends Person(id, name, age) {
  val _tId: String = tId

  def teaching(): Unit = {
    println("老师会上课")
  }

  override def sleep(): Unit = {
    println("老师会睡觉")
  }

  override def printInfo(): Unit = {
    println(s"Teacher($id,$tId,$name,$age)")
  }


}


object Demo11ScalaClassExtends {
  // 定义一个方法名为sleepTwice

//  // 针对学生类
//  def stuSleepTwice(stu:Student):Unit = {
//    stu.sleep()
//    stu.sleep()
//  }
//
//  // 针对老师类
//  def teaSleepTwice(teacher: Teacher): Unit = {
//    teacher.sleep()
//    teacher.sleep()
//  }

  // 针对人类
  def personSleepTwice(person: Person): Unit = {
    person.sleep()
    person.sleep()
  }

  def main(args: Array[String]): Unit = {
    val person: Person = new Person("01", "zs", 20)
    println(person._id)
    println(person._name)
    println(person._age)
    person.sleep()
    person.printInfo()
    println("=" * 30)
    val student: Student = new Student("001", "s01", "xh", 22)
    println(student._id)
    println(student._sId)
    println(student._name)
    println(student._age)
    student.sleep()
    student.printInfo()
    student.learning()
    println("=" * 30)
    val teacher: Teacher = new Teacher("0001", "t01", "zls", 26)
    println(teacher._id)
    println(teacher._tId)
    println(teacher._name)
    println(teacher._age)
    teacher.sleep()
    teacher.printInfo()
    teacher.teaching()
    println("=" * 30)

    // 多态：父类引用指向子类对象
    // 对同一类的对象（同一父类下的子类对象）调用相同的方法时，所表现出来的形态各不相同
    // 如果要形成多态，必须建立在继承的基础之上，并且在子类中要“重写”父类的方法
    personSleepTwice(student)
    personSleepTwice(teacher)
    personSleepTwice(person)

    println(person)

  }

}
